﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DevKnightlie.WPF.AutoForm.FieldDefinitions;

namespace DevKnightlie.WPF.AutoForm.Tests
{
    [TestClass]
    public class FieldDefinitionRegistryTests
    {

        /// <summary>
        /// Tests that default registrations are applied and returned
        /// </summary>
        [TestMethod]
        public void ReturnsDefaultDefinitions()
        {
            FieldDefinitionRegistry reg = FieldDefinitionRegistry.Default;
            Assert.IsTrue(reg.Definition<String>().GetType() == typeof(TextFieldDefinition));
            Assert.IsTrue(reg.Definition<int>().GetType() == typeof(IntegerFieldDefinition));
            Assert.IsTrue(reg.Definition<double>().GetType() == typeof(FloatFieldDefinition));
            Assert.IsTrue(reg.Definition<bool>().GetType() == typeof(CheckBoxFieldDefinition));
        }

        /// <summary>
        /// Tests that a new default definition for a data type is registered and returned
        /// </summary>
        [TestMethod]
        public void RegistersNewDefaultDefinition()
        {
            FieldDefinitionRegistry reg = FieldDefinitionRegistry.Create();
            reg.RegisterDefinition<String, TextFieldDefinition>();
            Assert.IsTrue(reg.Definition<String>().GetType() == typeof(TextFieldDefinition));
        }


        /// <summary>
        /// Tests that a new definition for a named property is registered and returned
        /// </summary>
        [TestMethod]
        public void RegistersNewPropertyDefinition()
        {
            FieldDefinitionRegistry reg = FieldDefinitionRegistry.Create();
            reg.RegisterDefinition<String, TextFieldDefinition>("TestProperty");
            Assert.IsTrue(reg.Definition<String>("TestProperty").GetType() == typeof(TextFieldDefinition));
        }

        /// <summary>
        /// Tests that property names preceded by a wildcard * are matched
        /// </summary>
        [TestMethod]
        public void MatchesPreWildcardProperties()
        {
            FieldDefinitionRegistry reg = FieldDefinitionRegistry.Create();
            reg.RegisterDefinition<String, PasswordFieldDefinition>("*Password");
            BaseFieldDefinition def = reg.Definition<String>("NewPassword");
            Assert.IsNotNull(def);
            Assert.IsTrue(def.GetType()==typeof(PasswordFieldDefinition));
            def = reg.Definition<String>("ConfirmPassword");
            Assert.IsNotNull(def);
            Assert.IsTrue(def.GetType() == typeof(PasswordFieldDefinition));
        }


        /// <summary>
        /// Tests that property names ending in a wildcard * are matched
        /// </summary>
        [TestMethod]
        public void MatchesPostWildcardProperties()
        {
            FieldDefinitionRegistry reg = FieldDefinitionRegistry.Create();
            reg.RegisterDefinition<int, IntegerSliderFieldDefinition>("Amount*");
            BaseFieldDefinition def = reg.Definition<int>("AmountOfTests");
            Assert.IsNotNull(def);
            Assert.IsTrue(def.GetType() == typeof(IntegerSliderFieldDefinition));
            def = reg.Definition<int>("AmountOfFailures");
            Assert.IsNotNull(def);
            Assert.IsTrue(def.GetType() == typeof(IntegerSliderFieldDefinition));
        }

    }
}
